# -*- coding: utf-8 -*-
import scrapy

from scrapy.http import FormRequest
from scrapy_ndd.items import ScrapyNddCategoryItem
from scrapy.loader import ItemLoader
from scrapy_ndd.utils.common import get_create_time
# from scrapy.utils.project import get_project_settings
from scrapy_ndd.utils.common import get_conf


class PPXCategorySpider(scrapy.Spider):

    name = 'ppx_category'
    settings = get_conf()
    ppx_host = settings.get('PPX_HOST', '')
    allowed_domains = [ppx_host.split('://')[-1].split('/')[0]]
    # start_urls = ['https://www.ppx29.com/']

    def start_requests(self):
        ppx_url = "{host}/categories/".format(host=self.ppx_host)

        yield FormRequest(ppx_url,
                          callback=self.parse_category)

    def parse_category(self, response):
        """
        爬分类
        :param response:
        :return:
        """
        post_nodes = response.css('#list_categories_categories_list_items a')
        for post_node in post_nodes:
            yield self.parse_category_details(response, post_node)

    def parse_category_details(self, response, post_node):
        item_loader = ItemLoader(item=ScrapyNddCategoryItem(), response=response)

        item_loader.add_value("cate_url", post_node.css("div.img img::attr(src)").extract())
        item_loader.add_value("cate_name", post_node.css("strong::text").extract())
        item_loader.add_value("cate_count", post_node.css("div.wrap div.videos::text").extract())
        item_loader.add_value("hoting_rate", post_node.css("div.wrap div:not(.videos)::text").extract())
        item_loader.add_value("item_href", post_node.css("a::attr(href)").extract())
        item_loader.add_value("create_time", get_create_time())
        item_loader.add_value("curr_page", [0])
        item_loader.add_value("curr_page_item", [0])
        item_loader.add_value("complete", [0])

        ndd_item = item_loader.load_item()
        return ndd_item
